/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateConnectionView.java
 *
 * Created on 2011-04-19, 19:16:50
 */

package cegg;

import cegg.graph.Graph;
import cegg.graph.vertex.Vertex;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.relation.RelationException;
import org.jdesktop.application.Action;

/**
 *
 * @author kumper
 */
public class CreateConnectionView extends javax.swing.JDialog {

    private Graph graph;

    /** Creates new form CreateConnectionView */
    public CreateConnectionView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public CreateConnectionView(java.awt.Frame parent, boolean modal, Graph graph) {
        super(parent, modal);
        initComponents();
        this.graph = graph;
    }

    @Override
    public void setVisible(boolean b) {
        verticesFromComboBox.removeAllItems();
        verticesToComboBox.removeAllItems();

        for (Vertex v : graph.getReasonVertices())
        {
            verticesFromComboBox.addItem(v);
        }
        for (Vertex v : graph.getIntermediateVertices())
        {
            verticesFromComboBox.addItem(v);
            verticesToComboBox.addItem(v);
        }
        for (Vertex v : graph.getEffectVertices())
        {
            verticesToComboBox.addItem(v);
        }

        super.setVisible(b);
    }

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        connectionTypePanel = new javax.swing.JPanel();
        connectionTypeLabel = new javax.swing.JLabel();
        connectionTypeComboBox = new javax.swing.JComboBox();
        verticesPanel = new javax.swing.JPanel();
        verticesFromLabel = new javax.swing.JLabel();
        verticesToLabel = new javax.swing.JLabel();
        verticesFromComboBox = new javax.swing.JComboBox();
        verticesToComboBox = new javax.swing.JComboBox();
        cancelButton = new javax.swing.JButton();
        connectButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        connectionTypePanel.setName("connectionTypePanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cegg.CeggApp.class).getContext().getResourceMap(CreateConnectionView.class);
        connectionTypeLabel.setText(resourceMap.getString("connectionTypeLabel.text")); // NOI18N
        connectionTypeLabel.setName("connectionTypeLabel"); // NOI18N

        connectionTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "equal", "not", "or", "and" }));
        connectionTypeComboBox.setName("connectionTypeComboBox"); // NOI18N

        javax.swing.GroupLayout connectionTypePanelLayout = new javax.swing.GroupLayout(connectionTypePanel);
        connectionTypePanel.setLayout(connectionTypePanelLayout);
        connectionTypePanelLayout.setHorizontalGroup(
            connectionTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, connectionTypePanelLayout.createSequentialGroup()
                .addContainerGap(66, Short.MAX_VALUE)
                .addComponent(connectionTypeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(connectionTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(70, 70, 70))
        );
        connectionTypePanelLayout.setVerticalGroup(
            connectionTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectionTypePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(connectionTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(connectionTypeLabel)
                    .addComponent(connectionTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        verticesPanel.setName("verticesPanel"); // NOI18N

        verticesFromLabel.setText(resourceMap.getString("verticesFromLabel.text")); // NOI18N
        verticesFromLabel.setName("verticesFromLabel"); // NOI18N

        verticesToLabel.setText(resourceMap.getString("verticesToLabel.text")); // NOI18N
        verticesToLabel.setName("verticesToLabel"); // NOI18N

        verticesFromComboBox.setName("verticesFromComboBox"); // NOI18N

        verticesToComboBox.setName("verticesToComboBox"); // NOI18N

        javax.swing.GroupLayout verticesPanelLayout = new javax.swing.GroupLayout(verticesPanel);
        verticesPanel.setLayout(verticesPanelLayout);
        verticesPanelLayout.setHorizontalGroup(
            verticesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(verticesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(verticesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(verticesPanelLayout.createSequentialGroup()
                        .addComponent(verticesFromComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                        .addComponent(verticesToComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(verticesPanelLayout.createSequentialGroup()
                        .addComponent(verticesFromLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 177, Short.MAX_VALUE)
                        .addComponent(verticesToLabel)))
                .addContainerGap())
        );
        verticesPanelLayout.setVerticalGroup(
            verticesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(verticesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(verticesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(verticesFromLabel)
                    .addComponent(verticesToLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(verticesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(verticesToComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(verticesFromComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cegg.CeggApp.class).getContext().getActionMap(CreateConnectionView.class, this);
        connectButton.setAction(actionMap.get("connectVerticesAction")); // NOI18N
        connectButton.setText(resourceMap.getString("connectButton.text")); // NOI18N
        connectButton.setName("connectButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(verticesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(connectionTypePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(connectButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(connectionTypePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(verticesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(connectButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CreateConnectionView dialog = new CreateConnectionView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void connectVerticesAction() {
        try {
            final String connectionType = connectionTypeComboBox.getSelectedItem().toString();

            if ("equal".equals(connectionType))
            {
                graph.connectEqual(((Vertex) verticesFromComboBox.getSelectedItem()).getId(),
                                   ((Vertex) verticesToComboBox.getSelectedItem()).getId());
            }
            if ("not".equals(connectionType))
            {
                graph.connectNot(((Vertex) verticesFromComboBox.getSelectedItem()).getId(),
                                 ((Vertex) verticesToComboBox.getSelectedItem()).getId());
            }
            if ("or".equals(connectionType))
            {
                graph.connectOr(((Vertex) verticesFromComboBox.getSelectedItem()).getId(),
                                ((Vertex) verticesToComboBox.getSelectedItem()).getId());
            }
            if ("and".equals(connectionType))
            {
                graph.connectAnd(((Vertex) verticesFromComboBox.getSelectedItem()).getId(),
                                 ((Vertex) verticesToComboBox.getSelectedItem()).getId());
            }
//            graph.connect(((Vertex) verticesFromComboBox.getSelectedItem()).getId(),
//                          ((Vertex) verticesToComboBox.getSelectedItem()).getId());
        } catch (RelationException ex) {
            Logger.getLogger(CreateConnectionView.class.getName()).log(Level.SEVERE, null, ex);
        }

        this.setVisible(false);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton connectButton;
    private javax.swing.JComboBox connectionTypeComboBox;
    private javax.swing.JLabel connectionTypeLabel;
    private javax.swing.JPanel connectionTypePanel;
    private javax.swing.JComboBox verticesFromComboBox;
    private javax.swing.JLabel verticesFromLabel;
    private javax.swing.JPanel verticesPanel;
    private javax.swing.JComboBox verticesToComboBox;
    private javax.swing.JLabel verticesToLabel;
    // End of variables declaration//GEN-END:variables

}
